package com.vichat.business.service.impl;

import com.vichat.business.entity.CommonAlarm;
import com.vichat.business.entity.VcAlarm;
import com.vichat.business.service.IVcAlarmService;
import com.vichat.common.util.NumberUtils;
import com.vichat.common.vo.PageFilter;
import com.vichat.core.dao.BaseDaoI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.vichat.common.vo.PageFilter.orderHql;

@Service("vcAlarmService")
public class VcAlarmServiceImpl implements IVcAlarmService {

    @Autowired
    private BaseDaoI<VcAlarm> vcAlarmDao;
    @Autowired
    private BaseDaoI<CommonAlarm> commonAlarmDao;

    public VcAlarm getAlarmByUid(long uid, long alarmState) {
        String hql = "from VcAlarm where state = 1 and uid = :uid";
        HashMap params = new HashMap();
        params.put("uid", uid);
        if (NumberUtils.isNullOrZero(alarmState)) {
            hql += " and alarmState in (1,4)";
        } else {
            hql += " and alarmState = :alarmState";
            params.put("alarmState", alarmState);
        }
        List<VcAlarm> vcAlarms = vcAlarmDao.find(hql, params);
        if (vcAlarms != null && vcAlarms.size() > 0) {
            return vcAlarms.get(0);
        }
        return null;
    }

    public VcAlarm getAlarmByalarmId(long alarmId, long alarmState) {
        String hql = "from VcAlarm where state = 1 and alarmId = :alarmId";
        HashMap params = new HashMap();
        params.put("alarmId", alarmId);
        if (NumberUtils.isNullOrZero(alarmState)) {
            hql += " and alarmState in (1,4)";
        } else {
            hql += " and alarmState = :alarmState";
            params.put("alarmState", alarmState);
        }
        List<VcAlarm> vcAlarms = vcAlarmDao.find(hql, params);
        if (vcAlarms != null && vcAlarms.size() > 0) {
            return vcAlarms.get(0);
        }
        return null;
    }

    public VcAlarm getAlarmByalarmId(long alarmId) {
        String hql = "from VcAlarm where state = 1 and alarmId = :alarmId";
        HashMap params = new HashMap();
        params.put("alarmId", alarmId);
        List<VcAlarm> vcAlarms = vcAlarmDao.find(hql, params);
        if (vcAlarms != null && vcAlarms.size() > 0) {
            return vcAlarms.get(0);
        }
        return null;
    }

    public VcAlarm saveAlarm(VcAlarm vcAlarmFDB) {
        if (NumberUtils.isNotNullOrZero(vcAlarmFDB.getAlarmId())) {
            vcAlarmFDB.setUpdateTime(new Date());
            vcAlarmDao.update(vcAlarmFDB);
        } else {
            vcAlarmFDB.setAlarmType(1l);
            vcAlarmFDB.setAlarmState(1L);
            vcAlarmFDB.setAlarmTime(new Date());
            vcAlarmFDB.setCreateTime(new Date());
            vcAlarmFDB.setUpdateTime(new Date());
            vcAlarmDao.save(vcAlarmFDB);
        }
        return vcAlarmFDB;
    }

    public List<VcAlarm> queryAlarms(VcAlarm queryBean, PageFilter pf) {
        HashMap params = new HashMap();
        String hql = "from VcAlarm where  state = 1  ";
        if (NumberUtils.isNotNullOrZero(queryBean.getAlarmId())) {
            hql += " and alarmId = :alarmId ";
            params.put("alarmId", queryBean.getAlarmId());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getUid())) {
            hql += " and uid = :uid ";
            params.put("uid", queryBean.getUid());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getRid())) {
            hql += " and rid = :rid ";
            params.put("rid", queryBean.getRid());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getPartyId())) {
            hql += " and partyId = :partyId ";
            params.put("partyId", queryBean.getPartyId());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getLocType())) {
            hql += " and locType = :locType ";
            params.put("locType", queryBean.getLocType());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getAlarmType())) {
            hql += " and alarmType = :alarmType ";
            params.put("alarmType", queryBean.getAlarmType());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getAlarmState())) {
            hql += " and alarmState = :alarmState ";
            params.put("alarmState", queryBean.getAlarmState());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getDealUid())) {
            hql += " and dealUid = :dealUid ";
            params.put("dealUid", queryBean.getDealUid());
        }
        if (StringUtils.isNotBlank(queryBean.getAddress())) {
            hql += " and address like :address ";
            params.put("address", "%" + queryBean.getAddress() + "%");
        }
        if (StringUtils.isNotBlank(queryBean.getDealResult())) {
            hql += " and dealResult like :dealResult ";
            params.put("dealResult", "%" + queryBean.getDealResult() + "%");
        }
        if (queryBean.getStartDate() != null) {
            hql += " and alarmTime >= :startDate ";
            params.put("startDate", queryBean.getStartDate());
        }
        if (queryBean.getEndDate() != null) {
            hql += " and alarmTime <= :endDate ";
            params.put("endDate", queryBean.getEndDate());
        }
        hql += " order by alarmId desc ";
        List<VcAlarm> vcAlarms = vcAlarmDao.find(hql, params, pf.getPage(), pf.getLimit());
        return vcAlarms;
    }

    public Long countAlarms(VcAlarm queryBean) {
        HashMap params = new HashMap();
        String hql = "from VcAlarm where  state = 1  ";
        if (NumberUtils.isNotNullOrZero(queryBean.getAlarmId())) {
            hql += " and alarmId = :alarmId ";
            params.put("alarmId", queryBean.getAlarmId());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getUid())) {
            hql += " and uid = :uid ";
            params.put("uid", queryBean.getUid());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getRid())) {
            hql += " and rid = :rid ";
            params.put("rid", queryBean.getRid());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getPartyId())) {
            hql += " and partyId = :partyId ";
            params.put("partyId", queryBean.getPartyId());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getLocType())) {
            hql += " and locType = :locType ";
            params.put("locType", queryBean.getLocType());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getAlarmType())) {
            hql += " and alarmType = :alarmType ";
            params.put("alarmType", queryBean.getAlarmType());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getAlarmState())) {
            hql += " and alarmState = :alarmState ";
            params.put("alarmState", queryBean.getAlarmState());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getDealUid())) {
            hql += " and dealUid = :dealUid ";
            params.put("dealUid", queryBean.getDealUid());
        }
        if (StringUtils.isNotBlank(queryBean.getAddress())) {
            hql += " and address like :address ";
            params.put("address", "%" + queryBean.getAddress() + "%");
        }
        if (StringUtils.isNotBlank(queryBean.getDealResult())) {
            hql += " and dealResult like :dealResult ";
            params.put("dealResult", "%" + queryBean.getDealResult() + "%");
        }
        if (queryBean.getStartDate() != null) {
            hql += " and dealTime >= :startDate ";
            params.put("startDate", queryBean.getStartDate());
        }
        if (queryBean.getEndDate() != null) {
            hql += " and dealTime <= :endDate ";
            params.put("endDate", queryBean.getEndDate());
        }
        return vcAlarmDao.count("select count(*) " + hql, params);
    }

    public List<VcAlarm> queryAlarmsByOrgId(long orgId) {
        String hql = "from VcAlarm where state = 1 and alarmState = 1 and orgId = :orgId";
        HashMap params = new HashMap();
        params.put("orgId", orgId);
        List<VcAlarm> vcAlarms = vcAlarmDao.find(hql, params);
        return vcAlarms;
    }

    public void saveDealResult(Long alarmId, String dealResult) {
        VcAlarm alarm = vcAlarmDao.get(VcAlarm.class, alarmId);
        if (alarm != null) {
            alarm.setDealResult(dealResult);
            vcAlarmDao.update(alarm);
        }
    }

    public CommonAlarm saveCommonAlarm(CommonAlarm vcAlarmFDB) {
        if (NumberUtils.isNotNullOrZero(vcAlarmFDB.getAlarmId())) {
            vcAlarmFDB.setUpdateTime(new Date());
            commonAlarmDao.update(vcAlarmFDB);
        } else {
            vcAlarmFDB.setAlarmState(1L);
            vcAlarmFDB.setCreateTime(new Date());
            vcAlarmFDB.setUpdateTime(new Date());
            commonAlarmDao.save(vcAlarmFDB);
        }
        return vcAlarmFDB;
    }

    public void delCommonAlarm(long alarmId) {
        CommonAlarm alarm = getCommonAlarmByalarmId(alarmId);
        if (alarm != null) {
            alarm.setState(0L);
            alarm.setUpdateTime(new Date());
            commonAlarmDao.update(alarm);
        }
    }

    public CommonAlarm getCommonFenceAlarmByUidObjId(long uid, long objId) {
        String hql = "from CommonAlarm where uid = :uid and objType=1 and objId=:objId";
        HashMap params = new HashMap();
        params.put("uid", uid);
        params.put("objId", objId);
        List<CommonAlarm> commonAlarmList = commonAlarmDao.find(hql, params);
        if (commonAlarmList != null && !commonAlarmList.isEmpty()) {
            return commonAlarmList.get(0);
        }
        return null;
    }

    public List<CommonAlarm> queryCommonAlarms(CommonAlarm queryBean, PageFilter pf) {
        HashMap params = new HashMap();
        String hql = "from CommonAlarm where  state = 1  ";
        if (NumberUtils.isNotNullOrZero(queryBean.getAlarmId())) {
            hql += " and alarmId = :alarmId ";
            params.put("alarmId", queryBean.getAlarmId());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getUid())) {
            hql += " and uid = :uid ";
            params.put("uid", queryBean.getUid());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getPartyId())) {
            hql += " and partyId = :partyId ";
            params.put("partyId", queryBean.getPartyId());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getLocType())) {
            hql += " and locType = :locType ";
            params.put("locType", queryBean.getLocType());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getAlarmType())) {
            hql += " and alarmType = :alarmType ";
            params.put("alarmType", queryBean.getAlarmType());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getAlarmState())) {
            hql += " and alarmState = :alarmState ";
            params.put("alarmState", queryBean.getAlarmState());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getDealUid())) {
            hql += " and dealUid = :dealUid ";
            params.put("dealUid", queryBean.getDealUid());
        }
        if (StringUtils.isNotBlank(queryBean.getAddress())) {
            hql += " and address like :address ";
            params.put("address", "%" + queryBean.getAddress() + "%");
        }
        if (StringUtils.isNotBlank(queryBean.getDealResult())) {
            hql += " and dealResult like :dealResult ";
            params.put("dealResult", "%" + queryBean.getDealResult() + "%");
        }
        if (queryBean.getStartDate() != null) {
            hql += " and alarmTime >= :startDate ";
            params.put("startDate", queryBean.getStartDate());
        }
        if (queryBean.getEndDate() != null) {
            hql += " and alarmTime <= :endDate ";
            params.put("endDate", queryBean.getEndDate());
        }
        hql += " order by alarmId desc ";
        List<CommonAlarm> vcAlarms = commonAlarmDao.find(hql, params, pf.getPage(), pf.getLimit());
        return vcAlarms;
    }

    public Long countCommonAlarms(CommonAlarm queryBean) {
        HashMap params = new HashMap();
        String hql = "from CommonAlarm where  state = 1  ";
        if (NumberUtils.isNotNullOrZero(queryBean.getAlarmId())) {
            hql += " and alarmId = :alarmId ";
            params.put("alarmId", queryBean.getAlarmId());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getUid())) {
            hql += " and uid = :uid ";
            params.put("uid", queryBean.getUid());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getPartyId())) {
            hql += " and partyId = :partyId ";
            params.put("partyId", queryBean.getPartyId());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getLocType())) {
            hql += " and locType = :locType ";
            params.put("locType", queryBean.getLocType());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getAlarmType())) {
            hql += " and alarmType = :alarmType ";
            params.put("alarmType", queryBean.getAlarmType());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getAlarmState())) {
            hql += " and alarmState = :alarmState ";
            params.put("alarmState", queryBean.getAlarmState());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getDealUid())) {
            hql += " and dealUid = :dealUid ";
            params.put("dealUid", queryBean.getDealUid());
        }
        if (StringUtils.isNotBlank(queryBean.getAddress())) {
            hql += " and address like :address ";
            params.put("address", "%" + queryBean.getAddress() + "%");
        }
        if (StringUtils.isNotBlank(queryBean.getDealResult())) {
            hql += " and dealResult like :dealResult ";
            params.put("dealResult", "%" + queryBean.getDealResult() + "%");
        }
        if (queryBean.getStartDate() != null) {
            hql += " and dealTime >= :startDate ";
            params.put("startDate", queryBean.getStartDate());
        }
        if (queryBean.getEndDate() != null) {
            hql += " and dealTime <= :endDate ";
            params.put("endDate", queryBean.getEndDate());
        }
        return commonAlarmDao.count("select count(*) " + hql, params);
    }

    @Override
    public void saveCommonAlarmDealResult(Long alarmId, String dealResult) {
        CommonAlarm alarm = commonAlarmDao.get(CommonAlarm.class, alarmId);
        if (alarm != null) {
            alarm.setDealResult(dealResult);
            commonAlarmDao.update(alarm);
        }
    }

    public CommonAlarm getCommonAlarmByalarmId(long alarmId, long alarmState) {
        String hql = "from CommonAlarm where state = 1 and alarmId = :alarmId";
        HashMap params = new HashMap();
        params.put("alarmId", alarmId);
        if (NumberUtils.isNullOrZero(alarmState)) {
            hql += " and alarmState in (1,4)";
        } else {
            hql += " and alarmState = :alarmState";
            params.put("alarmState", alarmState);
        }
        List<CommonAlarm> commonAlarms = commonAlarmDao.find(hql, params);
        if (commonAlarms != null && commonAlarms.size() > 0) {
            return commonAlarms.get(0);
        }
        return null;
    }

    public CommonAlarm getCommonAlarmByalarmId(long alarmId) {
        String hql = "from CommonAlarm where state = 1 and alarmId = :alarmId";
        HashMap params = new HashMap();
        params.put("alarmId", alarmId);
        List<CommonAlarm> commonAlarms = commonAlarmDao.find(hql, params);
        if (commonAlarms != null && commonAlarms.size() > 0) {
            return commonAlarms.get(0);
        }
        return null;
    }
}
